home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / zine / zine~2.jav < prev    next >
Encoding:
Text File  |  1995-10-16  |  9.3 KB  |  415 lines

  1. /*
  2.  *Zine Zine Zine Zine Zine Zine Zine
  3.  * a demo program.
  4.  * Implements popups for comic book style messages
  5.  * on top of an image. 
  6.  * uses a crummy form of markup language
  7.  * designed only to be easy to parse with java
  8.  * (read quick n dirty)
  9.  *Zine (more...)
  10.   * an app by Johan van der Hoeven.
  11.   * johan@rosebud.com
  12.   * http://www.rosebud.com/rb/rbhome.html
  13.   * johan_van_der_Hoeven@terc.edu
  14.   * 
  15.   * can be viewed at:
  16.   * http://http://www.fanzine.se/java/jo/zine.html (zine stuff)
  17.   * http://teaparty.terc.edu/java/z/glob.html (network science education research)
  18. */
  19.  
  20.  
  21.  
  22.  
  23. import java.awt.*;
  24. import java.io.StreamTokenizer;
  25. import java.io.InputStream;
  26. import java.io.FileInputStream;
  27. import java.net.URL;  
  28. import java.util.Vector;
  29. import java.util.Enumeration; 
  30.    class Point
  31.      {
  32.       
  33.       public int x=0,y=0;
  34.       public Point(int ax,int ay){x=ax;y=ay;};
  35.       public Point() { };
  36.  
  37.  
  38.      }
  39.  
  40.    class Rect
  41.    {
  42.  
  43.    public int lX=0;
  44.    public int tY=0;
  45.    public int rX=0;
  46.    public int bY=0;
  47.    public Rect(int leftX, int topY, int rightX, int bottomY)
  48.         {lX=leftX; tY=topY; rX=rightX; bY=bottomY;}
  49.  
  50.    public Rect(){};
  51.    public boolean isPointInRect(int x, int y)
  52.          {
  53.           if(x<rX&&x>lX&&y>tY&&    y<bY)
  54.            return true;
  55.            else
  56.            return false;
  57.  
  58.           }
  59.    public void offset(int dx, int dy)
  60.              {
  61.            lX+=dx;
  62.            rX+=dx;
  63.             tY+=dy;
  64.             bY+=dy;
  65.           };
  66.    public int    size().width()     { return (rX-lX);};
  67.    public int size().height()     { return (bY-tY);};
  68.    } 
  69.  
  70.  class zineFrame
  71.    {
  72.    public   Point p;
  73.    public    Rect r;
  74.    public    Vector strvec;    
  75.        /*  add more later...type etc.*/
  76.    
  77.    public zineFrame()
  78.          { r = new Rect(); strvec = new Vector(); p =new Point();
  79.          };
  80.    
  81.    }  
  82.   
  83.   /* you may wonder why there is a clipR when it is the same as textR*/
  84.   /* this is so that when the popup gets better looking the texrR */
  85.   /* will be a part of it */    
  86.  class zinePopUp
  87.   {
  88.    public Point p;   /* anchor */
  89.    public Rect clipR; /* area it covers */
  90.    public Rect textR; /*area for text*/
  91.    public zineFrame xfrm=null;
  92.    public Font font;
  93.    protected int lineHi, charWid;
  94.    
  95.  
  96.    public zinePopUp    (Font f) {
  97.                 p= new Point(); clipR = new Rect(); textR= new Rect();
  98.                  //font = new java.awt.Font("TimesRoman", Font.PLAIN, 12);
  99.                  font = f;
  100.                  charWid=font.stringWidth(" ");
  101.                  lineHi=font.height+2;
  102.                 };
  103.    public boolean doLayout(Rect boundR, zineFrame zf)
  104.           {
  105.            
  106.            xfrm=zf;
  107.            int wid=0;
  108.            int hi=0;
  109.             
  110.            for (Enumeration e = xfrm.strvec.elements(); e.hasMoreElements();)
  111.                     {
  112.                         hi+=lineHi;
  113.  
  114.                         String tmp=    (String) e.nextElement();
  115.  
  116.                          wid= Math.max(wid, font.stringWidth(tmp));
  117.                     
  118.                     
  119.                     }; 
  120.             wid+=2*charWid;
  121.            textR.bY=xfrm.p.y;
  122.            textR.lX=xfrm.p.x;
  123.            textR.rX=textR.lX+wid;
  124.            textR.tY=textR.bY-hi-lineHi/2;
  125.                doRectMunge(boundR);
  126.            clipR=textR;
  127.  
  128.           return true;
  129.           };
  130.    protected void doRectMunge(Rect boundR)
  131.           {
  132.           int xa=xfrm.p.x-boundR.lX;
  133.           int xb=boundR.rX-xfrm.p.x;
  134.           int ya=xfrm.p.y-boundR.tY;
  135.           int yb=boundR.bY-xfrm.p.y;
  136.  
  137.           /* check if it fit, if not try moving it. */
  138.         
  139.             if(textR.rX>boundR.rX)
  140.                {  
  141.                    //do a horizontal left shift
  142.                        int tmp= Math.min(textR.width(),xa);
  143.                    textR.offset(-tmp,0);
  144.                }
  145.                else
  146.              if(textR.lX<boundR.lX)
  147.                {
  148.                   //do a horizontal right shift
  149.                    int tmp= Math.min(textR.width(),xb);
  150.                    textR.offset(tmp,0);
  151.                }
  152.              /* vertical : (Has bugs! --OK for now....)*/
  153.                if(textR.tY<boundR.tY)
  154.                {
  155.                    //do a vertical down shift
  156.                     int tmp= Math.min(textR.height(),ya);
  157.                     textR.offset(0,tmp);
  158.                }
  159.                else
  160.                if(textR.bY>boundR.bY)
  161.                {
  162.             
  163.                    //do a vertical down shift
  164.                     int tmp= Math.min(textR.height(),yb);
  165.                    textR.offset(0,-tmp);
  166.                }
  167.  
  168.           };
  169.     
  170.    public void Draw(Graphics g)
  171.           {
  172.            g.setFont(font);
  173.            g.setColor(Color.lightGray);
  174.            g.fill3DRect(textR.lX, textR.tY, textR.width(), textR.height(),true);
  175.            g.setColor(Color.black);
  176.                int y=textR.tY;
  177.            for (Enumeration e = xfrm.strvec.elements(); e.hasMoreElements();)
  178.                     {
  179.                         y+=lineHi;
  180.                         g.drawString((String) e.nextElement() , textR.lX+charWid, y);
  181.                    
  182.                     }; 
  183.  
  184.           };
  185.   
  186.   } 
  187.    class Zine extends java.applet.Applet {
  188.         
  189.         protected String dana = null;
  190.         protected String imgna = null;
  191.         protected String shot=null;
  192.         protected Image  daimg;
  193.         Rect clipR;
  194.         Vector framevect;
  195.         boolean showhot=false;
  196.     
  197.         zineFrame cur_znfrm=null;
  198.         zinePopUp pop=null;
  199.         Font font;
  200.         public void init() {
  201.                             font = new java.awt.Font("TimesRoman", Font.PLAIN, 12);
  202.                             framevect= new Vector(); 
  203.                             resize(200, 200);
  204.                             dana = getParameter("dataurl");
  205.                             getFrames();
  206.  
  207.                             imgna = getParameter("imgurl");
  208.                             daimg= getImage(getCodeBase(), imgna);
  209.                             if(daimg!=null)
  210.                              resize(daimg.width,daimg.height);
  211.                             else
  212.                              resize(10, 10);
  213.                              shot = getParameter("showhot");
  214.  
  215.                              if(shot!=null)
  216.                              if(shot.equals("yes"))
  217.                                     showhot=true;
  218.                             }
  219.     
  220.               public void paint(Graphics g) {
  221.     
  222.                     
  223.                  
  224.                  g.drawImage(daimg, 0, 0, this);    
  225.                                  
  226.                 /*
  227.                     System.out.println("Zine Paint");
  228.                     System.out.println(zfrm);
  229.                     System.out.println(zfrm.p);
  230.                     System.out.println(zfrm.r);
  231.                     System.out.println(zfrm.strvec);
  232.                     System.out.println(framevect);
  233.                     */
  234.                 
  235.                     
  236.             
  237.                 if(showhot)
  238.                 for ( Enumeration ee = framevect.elements(); ee.hasMoreElements();)
  239.                 {
  240.                    zineFrame fr= (zineFrame)ee.nextElement();
  241.                  
  242.                   g.draw3DRect(fr.r.lX, fr.r.tY, fr.r.width(), fr.r.height(),true);
  243.                 
  244.                 };
  245.         
  246.             if(pop!=null)
  247.                pop.Draw(g);    
  248.             g.draw3DRect(0, 0, size().width, size().height,true);
  249.  
  250.           }   
  251.            /* end paint */
  252.  
  253.      public void update(Graphics g) {
  254.            // Clip to the affected area
  255.                //make sure not outside applet: 
  256.             //(needed else it can paint outside applet --java bug ?)
  257.             int x=    Math.max(0,clipR.lX);
  258.             int y=  Math.max(0,clipR.tY);
  259.             int w= Math.min(size().width,clipR.width());
  260.             int h= Math.min(size().height,clipR.height());
  261.     g.clipRect(clipR.lX, clipR.tY, clipR.width(), clipR.height());
  262.               paint(g);
  263.                }
  264.     public boolean keyDown(java.awt.Event evt, int k)
  265.                 {
  266.                 System.out.println("key");
  267.                  
  268.                    if(k=='s'||k=='S')
  269.                    {
  270.                       showhot=!showhot;
  271.                       clipR.lX=0;
  272.                       clipR.rX=size().width;
  273.                       clipR.tY=0;
  274.                       clipR.bY=size().height;
  275.                       repaint();
  276.                    }
  277.                 }
  278.     public boolean mouseEnter(java.awt.Event evt)
  279.                  { requestFocus();}
  280.  
  281.     public boolean mouseExit(java.awt.Event evt)
  282.                  { 
  283.                       if(pop!=null)
  284.                      {
  285.                       cur_znfrm=null;
  286.                       clipR=pop.clipR;
  287.                       pop=null;
  288.                       repaint();
  289.                       };
  290.                  
  291.                  }
  292.     public boolean mouseMove(java.awt.Event evt, int x, int y)
  293.                 {
  294.                     
  295.                 boolean change=false;
  296.                   if(cur_znfrm==null)
  297.                   {
  298.                         for ( Enumeration ee = framevect.elements(); ee.hasMoreElements();)
  299.                     {
  300.                      zineFrame fr= (zineFrame)ee.nextElement();
  301.                      if(fr.r.isPointInRect(x,y))
  302.                         { change=true;
  303.                           cur_znfrm=fr;
  304.                           pop = new zinePopUp(font);
  305.                           Rect r=new Rect(0,0,size().width,size().height);
  306.                           pop.doLayout(r,cur_znfrm);
  307.                           clipR=pop.clipR;
  308.                           };
  309.                     };
  310.                         
  311.                   }
  312.                    else
  313.                    {
  314.                   if(!cur_znfrm.r.isPointInRect(x,y))
  315.                      { 
  316.                      cur_znfrm=null;
  317.                      clipR=pop.clipR;
  318.                      pop=null;
  319.                       change=true;
  320.                      };
  321.                     };
  322.                      
  323.  
  324.                  if(change)
  325.                     {
  326.  
  327.                     repaint();
  328.  
  329.  
  330.                     };
  331.                 }  
  332.      
  333.      
  334.      
  335.      
  336.        protected void getFrames()
  337.                 {
  338.                                zineFrame zfrm=null;
  339.                             //System.out.println("zine get frames");
  340.                          
  341.                             InputStream is=null;
  342.  
  343.                             is = new URL(getDocumentBase(), dana).openStreamInteractively();
  344.                              
  345.                              StreamTokenizer st = new StreamTokenizer(is);
  346.                                 st.eolIsSignificant = false;
  347.                                 st.commentChar('#');
  348.                                 
  349.                                  
  350.                                  
  351.                                  /* parse file --NOT very clean can be improved alot */    
  352.                                int i=0;
  353.                               while (st.ttype != StreamTokenizer.TT_EOF)
  354.                                     {
  355.                                     st.nextToken();
  356.                                     
  357.                                            /* got a new frame: */
  358.                                     if(st.ttype == '{')
  359.                                       { 
  360.                                      
  361.                                       zfrm= new zineFrame(); 
  362.                                            i=0;
  363.                                         continue;
  364.                                        };
  365.                                      
  366.                                      if(st.ttype == '}')
  367.                                        {framevect.addElement(zfrm);
  368.                                          continue;
  369.                                        }
  370.  
  371.                                      if(st.ttype==st.TT_NUMBER)
  372.                                        {
  373.                                             int  n = (int) st.nval;
  374.                                         switch (i)
  375.                                               {
  376.                                             case 0:
  377.                                              zfrm.p.x=n;
  378.                                             break;
  379.                                             case 1:
  380.                                              zfrm.p.y=n;
  381.                                             break;
  382.                                             case 2:
  383.                                              zfrm.r.lX=n;
  384.                                             break;
  385.                                             case 3:
  386.                                              zfrm.r.tY=n;
  387.                                             break;
  388.                                             case 4:
  389.                                              zfrm.r.rX=n;
  390.                                             break;
  391.                                             case 5:
  392.                                              zfrm.r.bY=n;
  393.                                             break;
  394.  
  395.                                      
  396.                                             };     
  397.  
  398.                                             i++;
  399.                                             continue;
  400.                                        };
  401.  
  402.                                        if(st.ttype=='"')
  403.                                        { String tmp= st.sval;
  404.                                         zfrm.strvec.addElement(tmp);
  405.                                         continue;
  406.                                         };
  407.  
  408.                                     };    /* while */
  409.  
  410.                 };     /* end getFrames()    */
  411.  
  412.  
  413.      }   /* end zine class */ 
  414.    
  415.